package com.achobeta.recruitment.controller.wx;

import com.achobeta.recruitment.service.wx.WxService;
import com.achobeta.recruitment.utils.HttpUtils;
import com.achobeta.recruitment.utils.StringUtils;
import com.achobeta.recruitment.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-22
 * Time: 12:16
 */

@RestController
@Slf4j
public class WxController {

    @Autowired
    private WxService wxService;

    @GetMapping("/")
    public String check(@RequestParam("signature") String signature,
                        @RequestParam("timestamp") String timestamp,
                        @RequestParam("nonce") String nonce,
                        @RequestParam("echostr") String echostr) {
//        return echostr;
        if(!StringUtils.hasLength(signature, timestamp, nonce, echostr)) {
            return "";
        }
        return wxService.check(signature, timestamp, nonce, echostr);
    }
    @PostMapping("/") // 这个请求request来自微信服务器，所以不能用这个来获取登录用户的信息
    public String receiveMessage(HttpServletRequest request) throws IOException {
        String body = HttpUtils.getBody(request);
        Map<String, Object> map = XmlUtils.xmlToMap(body);
        System.out.println(map);
        // 获取回复消息
        String message = wxService.handleMessage(map);
        System.out.println(message);
        return message;
    }

}
